package org.callisto.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JPanel;

import org.callisto.EventType;
import org.callisto.model.Event;
import org.callisto.model.Keyboard;

public class Widget extends JPanel implements Observer {
	private static final long serialVersionUID = 1889055271738651612L;
	Keyboard keyboard;
	int width, activeWidth;
	int height, activeHeight;
	float aspectRatio = 1.0f;
	float padFactor = 0.9f;
	EventType eventType;
	float widgetMin, widgetMax;
	int stroke_index=0;
	
	public Widget(Keyboard keyboard, EventType eventType, int width, int height) {
		this.keyboard = keyboard;
		this.eventType = eventType;
		this.width = width;
		this.height = height;
		this.activeWidth = (int) (padFactor * width);
		this.activeHeight = (int) (padFactor * height);
		setPreferredSize(new Dimension(width, height));
		setMinimumSize(new Dimension(width * 3/2, height));
	}
		
	public void handleEvent(Event event) {
		if (event.getStatus() == this.eventType) {
			stroke_index++;
			repaint();
		}
	}
	
	public void update(Observable o, Object arg) {
		if (o instanceof Keyboard && arg instanceof Event) {
			try {
				handleEvent((Event) arg);
			} catch (Exception e) {
				System.out.println("Error executing MIDI event handler");
				e.printStackTrace();
			}
		} else {
			try {
				throw new Exception("Arguments must be of class Keyboard/Event");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public Keyboard getKeyboard() {
		return keyboard;
	}

	public void setKeyboard(Keyboard keyboard) {
		this.keyboard = keyboard;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public float getAspectRatio() {
		return aspectRatio;
	}

	public void setAspectRatio(float aspectRatio) {
		this.aspectRatio = aspectRatio;
	}

	public float getPadFactor() {
		return padFactor;
	}

	public void setPadFactor(float padFactor) {
		this.padFactor = padFactor;
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
	}
}
